<%@page import="user.Pm"%>
<%@page import="java.util.ArrayList"%>
<%@page import="user.User" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <link type="text/css" rel="stylesheet" href="sidebar.css" media="screen" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>Edit Profile Page</title>
        <%User userInfo = null;
                    userInfo = (User) session.getAttribute("userInfo");
                    boolean Send=false;

                    if(request.getParameter("username")!=null && request.getParameter("message")!=null &&userInfo!=null)
                    {String to=request.getParameter("username");
                    String message=request.getParameter("message");
                    Pm pm=new Pm(userInfo.getId(),userInfo.getFriendId(to),message);
                    if(userInfo!=null)
                    {userInfo.newPm(pm);
                    Send=true;}
                    }


                    //  int privMode=1;
                    ArrayList friends = null;
                    String username = null, name = null, surname = null, date = null, welcomeText = null;
                    String[] interests = null;

                    //   if (request.getParameter("username").equals(session.getAttribute("username"))) {//re-visit own profile
                    out.print("Welcome " + session.getAttribute("username") + "");
                    userInfo = (User) session.getAttribute("userInfo");
                    //      privMode=1;
                    //out.println("id " + session.getAttribute("id") + " </br>");
                    name = session.getAttribute("name").toString();
                    surname = session.getAttribute("surname").toString();
                    interests = session.getAttribute("interests").toString().split(",");
                    username = session.getAttribute("username").toString();
                    //        welcomeText=session.getAttribute("welcomeText").toString();
                    date = session.getAttribute("dateOfBirth").toString();

                    
                    userInfo.loadFriendList();
                  // friends = (ArrayList) session.getAttribute("friends");
                    friends = userInfo.getFriendUsernames();
                    if(friends==null)
                        friends.add("You Have No Friends");

                    //    }

        %>
    </head>
    <body>
        <div id ="footbar">
            <div id="RightBar">
                <br>
                <a href="endSession.jsp">
                    <b>
                        <input type="image" src="imadge/on-off.jpg" name="image" background-color="#C0C0C0" width="35" height="35" >
                    </b>
                </a>
            </div>
        </div>
        <div id="rightbar" class="sidebox">

            <table id="right">
                <tr> <td>
                        <hr>
                        <ul>

                            <%
                                        //if(userInfo.pendingFriendRequestsTo.length()>0)
                                        //  {out.print(userInfo.pendingFriendRequestsTo.length()+" users want to be your friends");
                                        // for(int i=0;i<userInfo.pendingFriendRequestsTo.length();i++)
                                        //    out.print("<a href=\"acceptFriend.jsp?username="+userInfo.pendingFriendRequestsTo.get(i)+"Accept</a> "
                                        //          +userInfo.pendingFriendRequestsTo.get(i));

                                        //     }
                                        if (friends != null) {
                                            out.println("Your Friends are " + friends.size() + "</br> ");
                                        }



                                        for (int i = 0; i < friends.size(); i++) {
                            %>

                            <li><b>

                                    <%out.println("<a href=profile.jsp?username=" + friends.get(i) + ">" + friends.get(i) + "</a></br>");
                                                }
                                                //   if(userInfo.pendingRequestsFromUser().length()>1)
                                                //         {for(int i=0;i<userInfo.pendingRequestsFromUser().length();i++)
                                                //   out.print("<a href=profile.jsp?username="+userInfo.pendingRequestsFromUser().get(i)+">"+"</a>");
                                                //              out.print("These users are not yet sure they want to be your friends");}
%>

                                </b></li>

                            <%

                                        String[] suggestions = userInfo.SuggestFriends();
                                        if (suggestions != null)
                                            if (suggestions.length >= 1)
                            %><li><% out.print("You should be friends with</br>");
                                %></li>><%
                                            for (int i = 0; i < suggestions.length; i++) {
                                %>

                            <li><b>
                                    <%out.print("<a href=profile.jsp?username=" + suggestions[i] + ">" + suggestions[i] + "</a></br>");
                                                }


                                    %>
                                    <a href="manageFriends.jsp">Manage Your Friends</a>
                                </b></li>
                        </ul>
                        <hr>
                        comon-popular </br>
                        interests list
                        <hr>
                    </td>
                </tr>
            </table>





        </div>
        <div id ="headbar">
            <h1>Abandonware <br/> Place</h1>
            <div id ="searchbar">
                <form action="search.jsp">
                    <input type="text" placeholder="search for usernames"/>
                    <a href="search.jsp"> Advanced Search</a>
                </form></div>
        </div>

        <div id="leftbar" class="sidebox">
                  <%if(userInfo!=null){%>
         <div style="z-index: 100%;">
             <a href="profile.jsp?username=<%out.print(session.getAttribute("username"));%>" >Go Back
<img src="imadge/pixel-mario.gif" alt="HTML tutorial" width="32" height="32" />
</a>
        </div>
        <%}else{%> <div style="z-index:100%;">
        <a href="index.jsp" >Go Back
<img src="imadge/pixel-mario.gif" alt="HTML tutorial" width="32" height="32" />
</a>
        </div>
        <%}%>

            <table style="width:5%;">
                <tr> <td>
                        <a href="profile.jsp"></a>
                        <hr>
                        <%

                                  
                                    out.println("</br>your personal info</br></br>");


                                    if (name != null) {
                                        out.println("NAME </br>" + name);
                                    }

                                    if (surname != null) {
                                        out.println(surname + "</br></br>");
                                    }
                                    // if(interests!=null)
                                    //   out.println(interests);
                                    if (username != null) {
                                        out.println("USERNAME</br>" + username + "</br></br>");
                                    }
                                    // if(welcomeText!=null)
                                    //   out.println(welcomeText);
                                    if (date != null) {
                                        out.println(date);
                                    }


                        %>

                        <!--               <b><input type="image" src="imadge/Pixel_Character_6__Yoshi_by_meowmixkitty1.jpg" name="image" width="55" height="55" >
                                  </b>
                        -->

                        <a href="editProfile.jsp">
                            <b>Edit</b>
                        </a>

                        <hr />
                        <% ArrayList temp = userInfo.getUnreadPms();
                    if (!temp.isEmpty()) {
                        out.println("<a href=readPms.jsp> You have " + temp.size() + "UnreadPms </a>");%>
                        <a href="readPms.jsp">
                            <b><input type="image" src="imadge/pms.jpg" name="pms" background-color="#C0C0C0" width="35" height="35" >
                            </b>
                        </a>
                        <hr />
                        <%}
                        %>            <a href="newPm.jsp">Sent A new Pm</a>

                        Interests
                        <ul>

                            <%for (int i = 0; i < interests.length; i++) {%>
                            <li><%out.print(interests[i]);%>
                            </li>    <%}%>

                        </ul>
                        <hr />
                    </td>


                </tr>
            </table>

        </div>

        <div id"mainContainer">
             <%
                         out.print(userInfo.getSuggestions().toString());
             %>

    </div>


    <a href="profile.jsp?username=northy">visit northy</a>
    <div id="text">


        <%if(Send){%>
        Your message Has been sent
        <%}else{%>
        <th> Your Personal Messages:</th>
        <th>


            <%

                        // lista apo ta pm toy ta diavasmena!!
                        if(userInfo!=null){
                        for(int i=0;i<userInfo.getReceivedPms().size();i++)
                         {Pm x=userInfo.getReceivedPms().get(i);
                            if(userInfo.getFriendUsername(x.from)!=null)
                                out.print("From "+userInfo.getFriendUsername(x.from)+" ");
                            out.print(x.text+" <br>");
                              
                        }
                        }
                       
            %>
     
    <tr>
        <th>You Have (<% out.println(userInfo.getUnreadPms().size());%>)New Messages<br><br></th>
        <th>
            <%
                        //lista apo a adiavasta mynhmata
                        if(userInfo!=null){
                        for(int i=0;i<userInfo.getUnreadPms().size();i++)
                         {Pm x=userInfo.getUnreadPms().get(i);
                            if(userInfo.getFriendUsername(x.from)!=null)
                                out.print("From "+userInfo.getFriendUsername(x.from));
                            out.print(x.text+"<br>");

                        }
                        }
                        
          }  %>

            Send a New Personal Message:
            <form name="NewPm" action="Pms.jsp" method="get">
                </br>To:</br>
                <input type="text" name="username" size="20"/>
                </br>Message:</br>
                <input type="text" name="message" size="100"/>
                 <input type="submit" value="Send"/>
            </form>
      


</div>


</body>
</html>
